/**
 * @author Can-Chen
 * @description 节点处理器，处理AST中的节点
 */

const signal = require('../signal');
const MemberValue = require('../value/memverValue')

const NodeHandler = {

    Program: function (nodeIterator) {
        for (const node of nodeIterator.node.body) {
            nodeIterator.traverse(node)
        }
    },

    ExpressionStatement: function (nodeIterator) {
        return nodeIterator.traverse(nodeIterator.node.expression)
    },

    CallExpression: function (nodeIterator) {
        const func = nodeIterator.traverse(nodeIterator.node.callee)
        const args = nodeIterator.node.arguments.map(arg => nodeIterator.traverse(arg))

        let value

        if (nodeIterator.node.callee.type === 'MemberExpression') {
            value = nodeIterator.traverse(nodeIterator.node.callee.object)
        }

        return func.apply(value, args)
    },

    MemberExpression: function (nodeIterator) {
        const obj = nodeIterator.traverse(nodeIterator.node.object)
        const name = nodeIterator.node.property.name
        return obj[name]
    },

    Identifier: function (nodeIterator) {
        if (nodeIterator.node.name === 'undefined') {
            return undefined
        }

        return nodeIterator.scope.get(nodeIterator.node.name).value
    },

    Literal: function (nodeIterator) {
        return nodeIterator.node.value
    },
}

module.exports = NodeHandler